package com.apex.wx.service.impl;

import com.apex.common.util.JsonUtils;
import com.apex.security.api.entity.MessageApprove;
import com.apex.security.api.entity.Pager;
import com.apex.wx.domain.MessageApproveRepository;
import com.apex.wx.service.MessageApproveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-4-8
 */
@Service
public class MessageApproveServiceImpl implements MessageApproveService {

    @Autowired
    private MessageApproveRepository repository;

    @Override
    public String index(Pager pager, String title, String userId) {
        int current=0;
        int rows=6;
        if(null!=pager){
            if(null!=pager.getCurrent()&&pager.getCurrent()>0){
                current=pager.getCurrent();
            }
            if(null!=pager.getRows()&&pager.getRows()>0){
                rows=pager.getRows();
            }
        }
        String code=repository.getByUserId(userId,current,rows,title);
        return code;
    }

    @Override
    public void entity(ModelMap modelMap, Integer id) {
        String code=repository.getEntity(id);
        MessageApprove messageApprove= JsonUtils.jsonEntity(code,MessageApprove.class);
        String messagehtml = messageApprove.getMessageHtml();
        //保留大部分样式，去掉字体，字号相关的
        messagehtml=messagehtml.replaceAll("FONT-FAMILY: .?.?;|FONT-SIZE: .?.?.?pt;|TEXT-INDENT: .?.?.?pt;","");
        messageApprove.setMessageHtml(messagehtml);
        modelMap.put("entity",messageApprove);
    }

    @Override
    public void noticeIndex(ModelMap modelMap,Pager pager, String title, String userId) {
        String code=index(pager,title,userId);
        List<MessageApprove> messageApproves=JsonUtils.jsonArray(code,MessageApprove.class);
        modelMap.put("dataList",messageApproves);
    }
}
